package Core.Map;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import Core.Character;

public class CombatGridImpl implements CombatGrid 
{
	private int _rowCount;
	private int _columnCount;
	private List<Tile> _tiles;
	
	public CombatGridImpl(int rowCount, int columnCount, Tile defaultTile)
	{
		this._rowCount = rowCount;
		this._columnCount = columnCount;
	
		this.initializeTiles(this._rowCount, this._columnCount, defaultTile);
	}

	@Override
	public int rowCount()
	{
		return this._rowCount;
	}

	@Override
	public int columnCount()
	{
		return this._columnCount;
	}

	@Override
	public List<Tile> tiles()
	{
		return this._tiles;
	}
	
	private void initializeTiles(int rowCount, int columnCount, Tile defaultTile)
	{
		this._tiles = new ArrayList<Tile>();
		
		for (int columnCounter = 1; columnCounter <= columnCount; columnCounter++)
		{
			for (int rowCounter = 1; rowCounter <= rowCount; rowCounter++)
			{
				Tile tile = defaultTile.clone();
				
				tile.setX(rowCounter);
				tile.setY(columnCounter);
				
				this._tiles.add(tile);
			}
		}
	}

	public Tile getTileWithCharacter(Character character)
	{
		Iterator<Tile> tileIterator = this.tiles().iterator();
		Tile foundTile = null;
		
		while (tileIterator.hasNext() && foundTile == null)
		{
			Tile currentTile = tileIterator.next();
			
			if (currentTile.containsCharacter(character))
			{
				foundTile = currentTile;
			}
		}
		
		return foundTile;
	}
	
	public Tile getTile(int x, int y)
	{
		Iterator<Tile> tileIterator = this.tiles().iterator();
		Tile foundTile = null;
		
		while (tileIterator.hasNext() && foundTile == null)
		{
			Tile currentTile = tileIterator.next();
			
			if (currentTile.getX() == x && currentTile.getY() == y)
			{
				foundTile = currentTile;
			}
		}
		
		return foundTile;
	}
}
